<?php

/**
 * User: htt
 * Date: 2019-02-20
 * Time: 上午10:54
 */

namespace App\Models\User;

use App\Models\Admin\UserAccessModel;
use App\Models\Model;
use Tymon\JWTAuth\Contracts\JWTSubject;
use Illuminate\Notifications\Notifiable;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Illuminate\Support\Facades\Cookie;

//class UserModel extends Authenticatable implements JWTSubject
class UserLevelModel extends Model
{


    protected $table = 'user_level';
    protected $fillable = ['id', 'uid', 'type','time'];

    /**
     * 等级类型
    */
    const  type_yipin = 1070;
    const  type_erpin = 1060;
    const  type_sanpin = 1050;
    const  type_sipin = 1040;
    const  type_wupin = 1030;
    const  type_xiaozuofang = 1020;
    const  type_yeyu = 1010;


    /**
     * 相应的价格
    */
    const  money_yipin = 300000;
    const  money_erpin = 50000;
    const  money_sanpin = 10000;
    const  money_sipin = 5000;
    const  money_wupin = 1000;
    const  money_xiaozuofang = 200;
    const  money_yeyu = 0;

    /*
    public static function getLevel($type=null){
        $list=[
            self::type_yipin=>'一品',
            self::type_erpin=>'二品',
            self::type_sanpin=>'三品',
            self::type_sipin=>'四品',
            self::type_wupin=>'五品',
            self::type_xiaozuofang=>'小作坊',
            self::type_yeyu=>'业余',

        ];
        if(!$type){
            return $list;
        }
        return isset($list[$type])?$list[$type]:'';
    }*/

    /**
     * 返回等级对应的金额
    */
    public static function getLevelInfo($type=null){
        $list=[
            self::type_yipin=>[
                'money'=>self::money_yipin,
                'name'=>'一级'
            ],
            self::type_erpin=>[
                'money'=>self::money_erpin,
                'name'=>'二级'
            ],
            self::type_sanpin=>[
                'money'=>self::money_sanpin,
                'name'=>'三级'
            ],
            self::type_sipin=>[
                'money'=>self::money_sipin,
                'name'=>'四级'
            ],
            self::type_wupin=>[
                'money'=>self::money_wupin,
                'name'=>'五级'
            ],
            self::type_xiaozuofang=>[
                'money'=>self::money_xiaozuofang,
                'name'=>'小作坊'
            ],
            self::type_yeyu=>[
                'money'=>self::money_yeyu,
                'name'=>'业余'
            ]
        ];
        if(!$type){
            return $list;
        }
        return isset($list[$type])?$list[$type]:0;
    }


    /**
     * 返回指定用户的类型
     * $returnType表示仅仅返回等级
    */
    public static function getUserLevel($uid,$returnType=false){
        $one=self::where('uid',$uid)->select('type')->first();
        $type=self::type_yeyu;
        if($one){
            $type=$one->type;
        }
        if($returnType){
            return $type;
        }
        return [
            'type'=>$type,
            'info'=>self::getLevelInfo($type)
        ];
    }

    /**
     * 修改用户等级状态
    */
    public static function upType($uid,$type){
        $one=self::where([
            'uid'=>$uid
        ])->first();
        if(!$one){
            $one=new self;
            $one->uid=$uid;
        }
        $one->time=time();
        $one->type=$type;
        return $one->save();
    }

}
